/**
 * @author Pieter Michels / wellconsidered
 * pieter@wellconsidered.be
 *
 * Open source under the GNU Lesser General Public License (http://www.opensource.org/licenses/lgpl-license.php)
 * Copyright © 2009 Pieter Michels / wellconsidered
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 
 * as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this library; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
 
package be.wellconsidered.apis.lighthouse.connectors
{
	import be.wellconsidered.apis.lighthouse.connectors.events.POSTEvents;
	
	import flash.events.Event;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	
	import mx.utils.Base64Encoder;
	
	public class POST extends AbstractConnector
	{
		private var _sToken:String = "";
		private var _xmlData:XML;
		
		private var _b64Credentials:Base64Encoder;
		
		public function POST(url:String, token:String, base64Credentials:Base64Encoder, xmlData:XML)
		{
			_sToken = token;
			_xmlData = xmlData;
		
			_b64Credentials = base64Credentials;
			
			super(url);
		}
		
		protected override function createConnector():void
		{
			super.createConnector();

			_request.requestHeaders.push(new URLRequestHeader("Content-Type", "application/xml")); // send xml
			_request.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + _b64Credentials.toString())); // authenticate
			_request.requestHeaders.push(new URLRequestHeader("Accept", "application/xml")); // accept xml as response
			
			_request.data = _xmlData;
			_request.method = URLRequestMethod.POST;
			
			super.load();
		}
		
		protected override function onConnectorComplete(e:Event):void
		{
			dispatchEvent(new POSTEvents(POSTEvents.COMPLETE, false, false, new XML(_loader.data)));
		}
	}
}